// import Home from '@/components/Home/index.tsx';
import { ConfigProvider,Alert } from 'antd';

import zhCN from 'antd/locale/zh_CN';
// import {Outlet,Link} from 'react-router-dom';
import router from './router/index.tsx';
import {useRoutes,useNavigate,useLocation } from 'react-router-dom';
import { useEffect , useState } from 'react';
import { req } from './common/methods';
import pub from '@/units/pub.ts';
import Loading from '@/components/Loading/index.tsx';

function App() {
  const navigate = useNavigate();
  const location = useLocation();
  const config = {
    autoInsertSpaceInButton:true,
    direction:'ltr',
    iconPrefixCls:'sheaneh-com',
    prefixCls:'sheaneh-com',
    locale:zhCN,
    theme:{
      token: {
        // Seed Token，影响范围大
        colorPrimary: '#6b00b9',
        borderRadius: 5,

        // 派生变量，影响范围小
        colorBgContainer: '#f6fff6',
      },
    }
  };
  const outlet = useRoutes(router);

  const [loading,setLoading] = useState(true);
  const [isError,setIsError] = useState(false);
  const [errMsg,setErrMsg] = useState('');
  const reqBaseData = () => {
    req('/api/get_login_state',{},'post').then(data => {      
      if(!data || typeof data !== 'object'){throw '初始化数据错误';}
      pub.setData('userid',data.userid);
      pub.setData('username',data.username);
      pub.setData('all_user',data.all_user);
      setLoading(false);
      //如果登录状态，跳转到/management/home
      //如果未登录状态，跳转到/nologin
      //alert(data.is_login);
      if(!!data.is_login){
        //如果登录
        if(location.pathname === '/nologin'){
          navigate('/management/home');
          message.info('您已登录。');
        }
      }else{
        if(location.pathname !== '/nologin'){
          navigate('/nologin');
          message.warning('您好，请先登录！');
        }
      }
    }).catch((error) => {
      setIsError(true);
      setErrMsg('' + error);
    })
  }
  useEffect(()=>{
    reqBaseData();
  },[])
  return (
    <>
      <ConfigProvider {...config}>
        {loading?(isError ? <><Alert
            message="错误"
            description={<pre>{''+errMsg}</pre>}
            type="error"
            showIcon
          /></> : <><Loading/></>):<div style={{minHeight:'100vh'}}>
          {/* <Home/> */}
          {/* <Outlet></Outlet> */}
          {outlet}
        </div>}
      </ConfigProvider>
      <div style={{display:'none'}}>王阳制作，请不要盗取！</div>
    </>
  )
}

export default App
